/**
 * Created with JetBrains WebStorm.
 * User: smd
 * Date: 2/10/14
 * Time: 8:02 AM
 * To change this template use File | Settings | File Templates.
 */

function ReportVO(rawData) {

    this.rawData = rawData;
    this.columns = [];
    this.rows = [];
    this.metaData;
    this.groupedList = [];
    this.rowsWithMain = [];

    this.groupByKey = 'ZEHFIRKPI__ZEHFIRKPI_KEY';
    this.groupByText = 'ZEHFIRKPI__ZEHFIRKPI_TXT';

    ReportVO.prototype.parse = function(){
        if(this.rawData !== undefined)
        {
            if(this.rawData != 'undefined')
            {
                this.columns = this.parseColumns(this.rawData.report.header);
                this.rowsWithMain = this.parseRows(this.rawData.report.data);
                this.metaData = this.parseMetaData(this.rawData.report.metadata);
                this.groupedList = this.getGroupList(this.rowsWithMain, this.groupByKey, this.groupByText);
                this.rows = this.removeRowsWithMain(this.rowsWithMain);

            }
        }
    }

    ReportVO.prototype.getGroupList = function(rows,groupByKey, groupByText)
    {
        var result = [];
        for(var i=0; i < rows.length; i++)
        {
            var data = rows[i];
            var temp = {};
            for (var prop in data) {
                if(prop == "ZFIRCOMP" && data['ZFIRCOMP'] == "MAIN")
                {
                    temp['value'] = data['4WMY63PQA3C718RMQ9AR0CZ8M'];
                    temp['text'] = data[groupByText];
                    temp['id'] = data[groupByKey];
                    result.push(temp);
                }
            }
        }

        /* remove duplicates */
        result = unique(result, 'id');
        return result;
    }

    ReportVO.prototype.removeRowsWithMain = function(rows) {
        var result = [];

        for( var x = 0 ; x < rows.length; x++)
        {
            if(rows[x]['ZFIRCOMP'] != "MAIN")
            {
                result.push(rows[x]);
            }
        }
        return result;
    }

    ReportVO.prototype.parseMetaData = function(rawJson){
        var result = new ReportMetaDataVO(rawJson);
        return result;
    }

    ReportVO.prototype.parseRows = function(rawJson){
        var result = [];
        if(rawJson != undefined)
        {
            for(var i=0; i < rawJson.length; i++)
            {
                var row = new ReportRowVO(rawJson[i].row.data);
                result.push(row);
            }
        }
        return result;
    }

    ReportVO.prototype.parseColumns = function(rawJson){
        var result = [];
        if(rawJson != undefined)
        {
            for(var i=0; i < rawJson.length; i++)
            {
                var column = new ReportColumnVO(rawJson[i].column);
                result.push(column);
            }
        }

        return result;
    }
}


function ReportMetaDataVO(data) {
    if(data != 'undefined')
    {
        for (var prop in data) {
            this[prop] = data[prop];
        }
    }
}

function ReportColumnVO(data) {
    if(data != 'undefined')
    {
        for (var prop in data) {
            this[prop] = data[prop];
        }
    }
}

function ReportRowVO(data) {
  if(data != 'undefined')
  {
      for (var prop in data) {
          this[prop] = data[prop];
      }
  }
}



